home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / Blobs / Blobs.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  41.6 KB  |  1,020 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: Blobs.cpp
  3. //
  4. // Desc: A pixel shader effect to mimic metaball physics in image space.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8. #include "dxstdafx.h"
  9. #include "resource.h"
  10.  
  11. //#define DEBUG_VS   // Uncomment this line to debug vertex shaders 
  12. //#define DEBUG_PS   // Uncomment this line to debug pixel shaders 
  13.  
  14.  
  15. //--------------------------------------------------------------------------------------
  16. // Constants
  17. //--------------------------------------------------------------------------------------
  18. #define NUM_BLOBS           5
  19. #define FIELD_OF_VIEW       ( (70.0f/90.0f)*(D3DX_PI/2.0f) )
  20.  
  21. #define GAUSSIAN_TEXSIZE    64
  22. #define GAUSSIAN_HEIGHT     1
  23. #define GAUSSIAN_DEVIATION  0.125f
  24.  
  25.  
  26. //--------------------------------------------------------------------------------------
  27. // Custom types
  28. //--------------------------------------------------------------------------------------
  29. // Vertex format for blob billboards
  30. struct POINTVERTEX  
  31. {
  32.     D3DXVECTOR3 pos;
  33.     float       size;
  34.     D3DXVECTOR3 color;
  35.  
  36.     static const DWORD FVF;
  37. };
  38. const DWORD POINTVERTEX::FVF = D3DFVF_XYZ | D3DFVF_PSIZE | D3DFVF_DIFFUSE; 
  39.  
  40.  
  41. // Vertex format for screen space work
  42. struct SCREENVERTEX     
  43. {
  44.     D3DXVECTOR4 pos;
  45.     D3DXVECTOR2 tCurr;
  46.     D3DXVECTOR2 tBack;
  47.     FLOAT       fSize;
  48.     D3DXVECTOR3 vColor;
  49.  
  50.     static const DWORD FVF;
  51. };
  52. const DWORD SCREENVERTEX::FVF = D3DFVF_XYZRHW | D3DFVF_TEX4
  53.                                 | D3DFVF_TEXCOORDSIZE2(0)
  54.                                 | D3DFVF_TEXCOORDSIZE2(1)
  55.                                 | D3DFVF_TEXCOORDSIZE1(2)
  56.                                 | D3DFVF_TEXCOORDSIZE3(3);
  57.  
  58.  
  59. struct CRenderTargetSet
  60. {
  61.     IDirect3DSurface9* apCopyRT[2];
  62.     IDirect3DSurface9* apBlendRT[2];
  63. };
  64.  
  65.  
  66. //--------------------------------------------------------------------------------------
  67. // Global variables
  68. //--------------------------------------------------------------------------------------
  69. ID3DXFont*              g_pFont = NULL;           // Font for drawing text
  70. ID3DXSprite*            g_pTextSprite = NULL;     // Sprite for batching draw text calls
  71. ID3DXEffect*            g_pEffect = NULL;         // D3DX effect interface
  72. CModelViewerCamera      g_Camera;                 // A model viewing camera
  73. bool                    g_bShowHelp = true;       // If true, it renders the UI control text
  74. CDXUTDialog             g_HUD;                    // dialog for standard controls
  75.  
  76. LPDIRECT3DVERTEXBUFFER9 g_pBlobVB = NULL;         // Vertex buffer for blob billboards
  77.  
  78. POINTVERTEX             g_BlobPoints[NUM_BLOBS];  // Position, size, and color states
  79.  
  80. LPDIRECT3DTEXTURE9      g_pTexGBuffer[4] = {0};   // Buffer textures for blending effect   
  81. LPDIRECT3DTEXTURE9      g_pTexScratch = NULL;     // Scratch texture
  82. LPDIRECT3DTEXTURE9      g_pTexBlob = NULL;        // Blob texture
  83. D3DFORMAT               g_BlobTexFormat;          // Texture format for blob texture
  84.  
  85. LPDIRECT3DCUBETEXTURE9  g_pEnvMap = NULL;         // Environment map   
  86.  
  87. int                     g_nPasses = 0;            // Number of rendering passes required
  88. int                     g_nRtUsed = 0;            // Number of render targets used for blending
  89. CRenderTargetSet        g_aRTSet[2];              // Render targets for each pass
  90. D3DXHANDLE              g_hBlendTech = NULL;      // Technique to use for blending
  91.  
  92.  
  93. //--------------------------------------------------------------------------------------
  94. // UI control IDs
  95. //--------------------------------------------------------------------------------------
  96. #define IDC_TOGGLEFULLSCREEN    1
  97. #define IDC_TOGGLEREF           3
  98. #define IDC_CHANGEDEVICE        4
  99.  
  100.  
  101. //--------------------------------------------------------------------------------------
  102. // Forward declarations 
  103. //--------------------------------------------------------------------------------------
  104. bool    CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed );
  105. void    CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps );
  106. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
  107. HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
  108. void    CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime );
  109. void    CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime );
  110. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing );
  111. void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown  );
  112. void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl );
  113. void    CALLBACK OnLostDevice();
  114. void    CALLBACK OnDestroyDevice();
  115.  
  116. HRESULT GenerateGaussianTexture();
  117. HRESULT FillBlobVB( D3DXMATRIXA16* pmatWorldView );
  118. HRESULT RenderFullScreenQuad( float fDepth );
  119. void    InitApp();
  120. void    RenderText();
  121.  
  122.  
  123.  
  124.  
  125. //--------------------------------------------------------------------------------------
  126. // Entry point to the program. Initializes everything and goes into a message processing 
  127. // loop. Idle time is used to render the scene.
  128. //--------------------------------------------------------------------------------------
  129. INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
  130. {
  131.     // Set the callback functions. These functions allow the sample framework to notify
  132.     // the application about device changes, user input, and windows messages.  The 
  133.     // callbacks are optional so you need only set callbacks for events you're interested 
  134.     // in. However, if you don't handle the device reset/lost callbacks then the sample 
  135.     // framework won't be able to reset your device since the application must first 
  136.     // release all device resources before resetting.  Likewise, if you don't handle the 
  137.     // device created/destroyed callbacks then the sample framework won't be able to 
  138.     // recreate your device resources.
  139.     DXUTSetCallbackDeviceCreated( OnCreateDevice );
  140.     DXUTSetCallbackDeviceReset( OnResetDevice );
  141.     DXUTSetCallbackDeviceLost( OnLostDevice );
  142.     DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
  143.     DXUTSetCallbackMsgProc( MsgProc );
  144.     DXUTSetCallbackKeyboard( KeyboardProc );
  145.     DXUTSetCallbackFrameRender( OnFrameRender );
  146.     DXUTSetCallbackFrameMove( OnFrameMove );
  147.  
  148.     // Show the cursor and clip it when in full screen
  149.     DXUTSetCursorSettings( true, true );
  150.  
  151.     InitApp();
  152.  
  153.     // Initialize the sample framework and create the desired Win32 window and Direct3D 
  154.     // device for the application. Calling each of these functions is optional, but they
  155.     // allow you to set several options which control the behavior of the framework.
  156.     DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
  157.     DXUTCreateWindow( L"Blobs" );
  158.     DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
  159.  
  160.     // Pass control to the sample framework for handling the message pump and 
  161.     // dispatching render calls. The sample framework will call your FrameMove 
  162.     // and FrameRender callback when there is idle time between handling window messages.
  163.     DXUTMainLoop();
  164.  
  165.     // Perform any application-level cleanup here. Direct3D device resources are released within the
  166.     // appropriate callback functions and therefore don't require any cleanup code here.
  167.  
  168.     return DXUTGetExitCode();
  169. }
  170.  
  171.  
  172. //--------------------------------------------------------------------------------------
  173. // Initialize the app 
  174. //--------------------------------------------------------------------------------------
  175. void InitApp()
  176. {
  177.     // Initialize dialogs
  178.     g_HUD.SetCallback( OnGUIEvent ); int iY = 10; 
  179.     g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
  180.     g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22 );
  181.     g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22 );
  182.  
  183.     // Set initial blob states
  184.     for( int i=0; i < NUM_BLOBS; i++ )
  185.     {
  186.         g_BlobPoints[i].pos = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  187.         g_BlobPoints[i].size = 1.0f;
  188.     }
  189.  
  190.     g_BlobPoints[0].color = D3DXVECTOR3(0.3f, 0.0f, 0.0f);
  191.     g_BlobPoints[1].color = D3DXVECTOR3(0.0f, 0.3f, 0.0f);
  192.     g_BlobPoints[2].color = D3DXVECTOR3(0.0f, 0.0f, 0.3f);
  193.     g_BlobPoints[3].color = D3DXVECTOR3(0.3f, 0.3f, 0.0f);
  194.     g_BlobPoints[4].color = D3DXVECTOR3(0.0f, 0.3f, 0.3f);
  195. }
  196.  
  197.  
  198. //--------------------------------------------------------------------------------------
  199. // Called during device initialization, this code checks the device for some 
  200. // minimum set of capabilities, and rejects those that don't pass by returning false.
  201. //--------------------------------------------------------------------------------------
  202. bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
  203.                                   D3DFORMAT BackBufferFormat, bool bWindowed )
  204. {
  205.     // Skip backbuffer formats that don't support alpha blending
  206.     IDirect3D9* pD3D = DXUTGetD3DObject(); 
  207.     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
  208.                     AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
  209.                     D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
  210.         return false;
  211.  
  212.     // No fallback, so need ps2.0
  213.     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
  214.         return false;
  215.  
  216.     // No fallback, so need to support render target
  217.     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
  218.                 AdapterFormat, D3DUSAGE_RENDERTARGET | D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
  219.                 D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
  220.     {
  221.         return false;
  222.     }
  223.     
  224.     // Check support for pixel formats that are going to be used
  225.     // D3DFMT_A16B16G16R16F render target
  226.     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
  227.                     AdapterFormat, D3DUSAGE_RENDERTARGET, 
  228.                     D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F ) ) )
  229.     {
  230.         return false;
  231.     }
  232.  
  233.     // D3DFMT_R32F render target
  234.     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
  235.                     AdapterFormat, D3DUSAGE_RENDERTARGET, 
  236.                     D3DRTYPE_TEXTURE, D3DFMT_R32F) ) )
  237.     {
  238.         return false;
  239.     }
  240.  
  241.     return true;
  242. }
  243.  
  244.  
  245. //--------------------------------------------------------------------------------------
  246. // This callback function is called immediately before a device is created to allow the 
  247. // application to modify the device settings. The supplied pDeviceSettings parameter 
  248. // contains the settings that the framework has selected for the new device, and the 
  249. // application can make any desired changes directly to this structure.  Note however that 
  250. // the sample framework will not correct invalid device settings so care must be taken 
  251. // to return valid device settings, otherwise IDirect3D9::CreateDevice() will fail.  
  252. //--------------------------------------------------------------------------------------
  253. void CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps )
  254. {
  255.     // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
  256.     // then switch to SWVP.
  257.     if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
  258.          pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
  259.     {
  260.         pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  261.     }
  262.     else
  263.     {
  264.         pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  265.     }
  266.  
  267.     // This application is designed to work on a pure device by not using 
  268.     // IDirect3D9::Get*() methods, so create a pure device if supported and using HWVP.
  269.     if ((pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 && 
  270.         (pDeviceSettings->BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING) != 0 )
  271.         pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;
  272.  
  273.     // Debugging vertex shaders requires either REF or software vertex processing 
  274.     // and debugging pixel shaders requires REF.  
  275. #ifdef DEBUG_VS
  276.     if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
  277.     {
  278.         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
  279.         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;                            
  280.         pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  281.     }
  282. #endif
  283. #ifdef DEBUG_PS
  284.     pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
  285. #endif
  286.  
  287. }
  288.  
  289.  
  290. //--------------------------------------------------------------------------------------
  291. // This callback function will be called immediately after the Direct3D device has been 
  292. // created, which will happen during application initialization and windowed/full screen 
  293. // toggles. This is the best location to create D3DPOOL_MANAGED resources since these 
  294. // resources need to be reloaded whenever the device is destroyed. Resources created  
  295. // here should be released in the OnDestroyDevice callback. 
  296. //--------------------------------------------------------------------------------------
  297. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  298. {
  299.     HRESULT hr;
  300.  
  301.     // Query multiple RT setting and set the num of passes required
  302.     D3DCAPS9 Caps;
  303.     pd3dDevice->GetDeviceCaps( &Caps );
  304.     if( Caps.NumSimultaneousRTs < 2 )
  305.     {
  306.         g_nPasses = 2;
  307.         g_nRtUsed = 1;
  308.     }
  309.     else
  310.     {
  311.         g_nPasses = 1;
  312.         g_nRtUsed = 2;
  313.     }
  314.  
  315.     // Determine which of D3DFMT_R16F or D3DFMT_R32F to use for blob texture
  316.     IDirect3D9* pD3D;
  317.     pd3dDevice->GetDirect3D( &pD3D );
  318.     D3DDISPLAYMODE DisplayMode;
  319.     pd3dDevice->GetDisplayMode( 0, &DisplayMode );
  320.  
  321.     if( FAILED( pD3D->CheckDeviceFormat( Caps.AdapterOrdinal, Caps.DeviceType,
  322.                     DisplayMode.Format, D3DUSAGE_RENDERTARGET, 
  323.                     D3DRTYPE_TEXTURE, D3DFMT_R16F) ) )
  324.         g_BlobTexFormat = D3DFMT_R32F;
  325.     else
  326.         g_BlobTexFormat = D3DFMT_R16F;
  327.  
  328.     SAFE_RELEASE( pD3D );
  329.  
  330.     // Initialize the font
  331.     V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, 
  332.                          OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, 
  333.                          L"Arial", &g_pFont ) );
  334.  
  335.     // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the 
  336.     // shader debugger. Debugging vertex shaders requires either REF or software vertex 
  337.     // processing, and debugging pixel shaders requires REF.  The 
  338.     // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the 
  339.     // shader debugger.  It enables source level debugging, prevents instruction 
  340.     // reordering, prevents dead code elimination, and forces the compiler to compile 
  341.     // against the next higher available software target, which ensures that the 
  342.     // unoptimized shaders do not exceed the shader model limitations.  Setting these 
  343.     // flags will cause slower rendering since the shaders will be unoptimized and 
  344.     // forced into software.  See the DirectX documentation for more information about 
  345.     // using the shader debugger.
  346.     DWORD dwShaderFlags = 0;
  347.     #ifdef DEBUG_VS
  348.         dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
  349.     #endif
  350.     #ifdef DEBUG_PS
  351.         dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
  352.     #endif
  353.  
  354.     // Read the D3DX effect file
  355.     WCHAR str[MAX_PATH];
  356.     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Blobs.fx" ) );
  357.  
  358.     // If this fails, there should be debug output as to 
  359.     // they the .fx file failed to compile
  360.     V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, 
  361.                                         NULL, &g_pEffect, NULL ) );
  362.  
  363.     // Initialize the technique for blending
  364.     if( 1 == g_nPasses )
  365.     {
  366.         // Multiple RT available
  367.         g_hBlendTech = g_pEffect->GetTechniqueByName( "BlobBlend" );
  368.     } else
  369.     {
  370.         // Single RT. Multiple passes.
  371.         g_hBlendTech = g_pEffect->GetTechniqueByName( "BlobBlendTwoPasses" );
  372.     }
  373.  
  374.     // Setup the camera's view parameters
  375.     D3DXVECTOR3 vecEye(0.0f, 0.0f, -5.0f);
  376.     D3DXVECTOR3 vecAt (0.0f, 0.0f, -0.0f);
  377.     g_Camera.SetViewParams( &vecEye, &vecAt );
  378.  
  379.     return S_OK;
  380. }
  381.  
  382.  
  383.  
  384.  
  385. //--------------------------------------------------------------------------------------
  386. // This callback function will be called immediately after the Direct3D device has been 
  387. // reset, which will happen after a lost device scenario. This is the best location to 
  388. // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever 
  389. // the device is lost. Resources created here should be released in the OnLostDevice 
  390. // callback. 
  391. //--------------------------------------------------------------------------------------
  392. HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
  393.                                 const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  394. {
  395.     HRESULT hr;
  396.  
  397.     if( g_pFont )
  398.         V_RETURN( g_pFont->OnResetDevice() );
  399.     if( g_pEffect )
  400.         V_RETURN( g_pEffect->OnResetDevice() );
  401.  
  402.     // Create a sprite to help batch calls when drawing many lines of text
  403.     V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
  404.  
  405.     // Create the Gaussian distribution texture
  406.     V_RETURN( GenerateGaussianTexture() );
  407.   
  408.     // Create the blob vertex buffer
  409.     V_RETURN( pd3dDevice->CreateVertexBuffer( NUM_BLOBS * 6 * sizeof(SCREENVERTEX), 
  410.                                                 D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,
  411.                                                 SCREENVERTEX::FVF, D3DPOOL_DEFAULT,
  412.                                                 &g_pBlobVB, NULL ) );
  413.     
  414.     // Create the blank texture
  415.     V_RETURN( pd3dDevice->CreateTexture( 1, 1, 1,
  416.                                            D3DUSAGE_RENDERTARGET,
  417.                                            D3DFMT_A16B16G16R16F,
  418.                                            D3DPOOL_DEFAULT,
  419.                                            &g_pTexScratch, NULL ) );
  420.  
  421.  
  422.     // Create buffer textures 
  423.     for( int i=0; i < 4; ++i )
  424.     {
  425.         V_RETURN( pd3dDevice->CreateTexture( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 1,
  426.                                                D3DUSAGE_RENDERTARGET,
  427.                                                D3DFMT_A16B16G16R16F,
  428.                                                D3DPOOL_DEFAULT, &g_pTexGBuffer[i], NULL ) );
  429.     }
  430.  
  431.     // Initialize the render targets
  432.     if( 1 == g_nPasses )
  433.     {
  434.         // Multiple RT
  435.         IDirect3DSurface9* pSurf;
  436.         g_pTexGBuffer[2]->GetSurfaceLevel( 0, &pSurf );
  437.         g_aRTSet[0].apCopyRT[0] = pSurf;
  438.         g_pTexGBuffer[3]->GetSurfaceLevel( 0, &pSurf );
  439.         g_aRTSet[0].apCopyRT[1] = pSurf;
  440.         g_pTexGBuffer[0]->GetSurfaceLevel( 0, &pSurf );
  441.         g_aRTSet[0].apBlendRT[0] = pSurf;
  442.         g_pTexGBuffer[1]->GetSurfaceLevel( 0, &pSurf );
  443.         g_aRTSet[0].apBlendRT[1] = pSurf;
  444.  
  445.         // 2nd pass is not needed. Therefore all RTs are NULL for this pass.
  446.         g_aRTSet[1].apCopyRT[0] = NULL;
  447.         g_aRTSet[1].apCopyRT[1] = NULL;
  448.         g_aRTSet[1].apBlendRT[0] = NULL;
  449.         g_aRTSet[1].apBlendRT[1] = NULL;
  450.     } else
  451.     {
  452.         // Single RT, multiple passes
  453.         IDirect3DSurface9* pSurf;
  454.         g_pTexGBuffer[2]->GetSurfaceLevel( 0, &pSurf );
  455.         g_aRTSet[0].apCopyRT[0] = pSurf;
  456.         g_pTexGBuffer[3]->GetSurfaceLevel( 0, &pSurf );
  457.         g_aRTSet[1].apCopyRT[0] = pSurf;
  458.         g_pTexGBuffer[0]->GetSurfaceLevel( 0, &pSurf );
  459.         g_aRTSet[0].apBlendRT[0] = pSurf;
  460.         g_pTexGBuffer[1]->GetSurfaceLevel( 0, &pSurf );
  461.         g_aRTSet[1].apBlendRT[0] = pSurf;
  462.  
  463.         // RT 1 is not available. Therefore all RTs are NULL for this index.
  464.         g_aRTSet[0].apCopyRT[1] = NULL;
  465.         g_aRTSet[1].apCopyRT[1] = NULL;
  466.         g_aRTSet[0].apBlendRT[1] = NULL;
  467.         g_aRTSet[1].apBlendRT[1] = NULL;
  468.     }
  469.  
  470.     // Set the camera parameters
  471.     float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
  472.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, -2.0f, -5.0f );
  473.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  474.     g_Camera.SetViewParams( &vEyePt, &vLookatPt );
  475.     g_Camera.SetProjParams( FIELD_OF_VIEW, fAspectRatio, 1.0f, 100.0f );
  476.     g_Camera.SetRadius( 5.0f, 2.0f, 20.0f  );
  477.     g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
  478.     g_Camera.SetInvertPitch( true );
  479.  
  480.     // Position the on-screen dialog
  481.     g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
  482.     g_HUD.SetSize( 170, 170 );
  483.     
  484.     return S_OK;
  485. }
  486.  
  487.  
  488. //-----------------------------------------------------------------------------
  489. // Name: GenerateGaussianTexture()
  490. // Desc: Generate a texture to store gaussian distribution results
  491. //-----------------------------------------------------------------------------
  492. HRESULT GenerateGaussianTexture()
  493. {
  494.     HRESULT hr = S_OK;
  495.     LPDIRECT3DSURFACE9 pBlobTemp = NULL;
  496.     LPDIRECT3DSURFACE9 pBlobNew = NULL;
  497.     
  498.     IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
  499.     
  500.     // Create a temporary texture
  501.     LPDIRECT3DTEXTURE9 texTemp;
  502.     V_RETURN( pd3dDevice->CreateTexture( GAUSSIAN_TEXSIZE, GAUSSIAN_TEXSIZE, 1,
  503.                                          D3DUSAGE_DYNAMIC, D3DFMT_R32F,
  504.                                          D3DPOOL_DEFAULT, &texTemp, NULL ) );
  505.     // Create the gaussian texture
  506.     V_RETURN( pd3dDevice->CreateTexture( GAUSSIAN_TEXSIZE, GAUSSIAN_TEXSIZE, 1,
  507.                                          D3DUSAGE_DYNAMIC, g_BlobTexFormat,
  508.                                          D3DPOOL_DEFAULT, &g_pTexBlob, NULL ) );
  509.    
  510.     // Create the environment map
  511.     TCHAR str[MAX_PATH];
  512.     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"lobby\\lobbycube.dds" ) );
  513.     V_RETURN( D3DXCreateCubeTextureFromFile( pd3dDevice, str, &g_pEnvMap ) );
  514.  
  515.     // Fill in the gaussian texture data
  516.     D3DLOCKED_RECT Rect;
  517.     V_RETURN( texTemp->LockRect(0, &Rect, 0, 0) );
  518.     
  519.     int u, v;
  520.     float dx, dy, I;
  521.     float* pBits;  
  522.     
  523.     for( v=0; v < GAUSSIAN_TEXSIZE; ++v )
  524.     {
  525.         pBits = (float*)((char*)(Rect.pBits)+v*Rect.Pitch);
  526.         
  527.         for( u=0; u < GAUSSIAN_TEXSIZE; ++u )
  528.         {
  529.             dx = 2.0f*u/(float)GAUSSIAN_TEXSIZE-1.0f;
  530.             dy = 2.0f*v/(float)GAUSSIAN_TEXSIZE-1.0f;
  531.             I = GAUSSIAN_HEIGHT * (float)exp(-(dx*dx+dy*dy)/GAUSSIAN_DEVIATION);
  532.  
  533.             *(pBits++) = I;  // intensity
  534.         }
  535.     }
  536.  
  537.     texTemp->UnlockRect(0);
  538.  
  539.     // Copy the temporary surface to the stored gaussian texture
  540.     V_RETURN( texTemp->GetSurfaceLevel( 0, &pBlobTemp ) );
  541.     V_RETURN( g_pTexBlob->GetSurfaceLevel( 0, &pBlobNew ) );
  542.     V_RETURN( D3DXLoadSurfaceFromSurface( pBlobNew, 0, 0, pBlobTemp, 0, 0, D3DX_FILTER_NONE, 0 ) );
  543.     
  544.     SAFE_RELEASE(pBlobTemp);
  545.     SAFE_RELEASE(pBlobNew);
  546.     SAFE_RELEASE(texTemp);
  547.  
  548.     return S_OK;
  549. }
  550.  
  551.  
  552. //-----------------------------------------------------------------------------
  553. // Name: FillBlobVB()
  554. // Desc: Fill the vertex buffer for the blob objects
  555. //-----------------------------------------------------------------------------
  556. HRESULT FillBlobVB( D3DXMATRIXA16* pmatWorldView )
  557. {
  558.     HRESULT hr = S_OK;
  559.     UINT i=0; // Loop variable
  560.     
  561.     SCREENVERTEX* pBlobVertex;
  562.     V_RETURN( g_pBlobVB->Lock( 0, 0, (void**)&pBlobVertex, D3DLOCK_DISCARD ) );
  563.     
  564.     POINTVERTEX blobPos[ NUM_BLOBS ];
  565.     
  566.     for( i=0; i < NUM_BLOBS; ++i )
  567.     {
  568.         //transform point to camera space
  569.         D3DXVECTOR4 blobPosCamera;
  570.         D3DXVec3Transform( &blobPosCamera, &g_BlobPoints[i].pos, pmatWorldView );
  571.         
  572.         blobPos[i] = g_BlobPoints[i];
  573.         blobPos[i].pos.x = blobPosCamera.x;
  574.         blobPos[i].pos.y = blobPosCamera.y;
  575.         blobPos[i].pos.z = blobPosCamera.z;
  576.     }
  577.  
  578.     int posCount=0;
  579.     for( i=0; i < NUM_BLOBS; ++i )
  580.     {
  581.         D3DXVECTOR4 blobScreenPos;
  582.  
  583.         // For calculating billboarding
  584.         D3DXVECTOR4 billOfs(blobPos[i].size,blobPos[i].size,blobPos[i].pos.z,1);
  585.         D3DXVECTOR4 billOfsScreen;
  586.  
  587.         // Transform to screenspace
  588.         const D3DXMATRIX* pmatProjection = g_Camera.GetProjMatrix();
  589.         D3DXVec3Transform(&blobScreenPos, &blobPos[i].pos, pmatProjection);
  590.         D3DXVec4Transform(&billOfsScreen, &billOfs, pmatProjection);
  591.  
  592.         // Project
  593.         D3DXVec4Scale(&blobScreenPos,&blobScreenPos,1.0f/blobScreenPos.w);
  594.         D3DXVec4Scale(&billOfsScreen,&billOfsScreen,1.0f/billOfsScreen.w);
  595.  
  596.  
  597.         D3DXVECTOR2 vTexCoords[] = 
  598.         {
  599.             D3DXVECTOR2(0.0f,0.0f),
  600.             D3DXVECTOR2(1.0f,0.0f),
  601.             D3DXVECTOR2(0.0f,1.0f),
  602.             D3DXVECTOR2(0.0f,1.0f),
  603.             D3DXVECTOR2(1.0f,0.0f),
  604.             D3DXVECTOR2(1.0f,1.0f),
  605.         };
  606.  
  607.         D3DXVECTOR4 vPosOffset[] =
  608.         {
  609.             D3DXVECTOR4(-billOfsScreen.x,-billOfsScreen.y,0.0f,0.0f),
  610.             D3DXVECTOR4( billOfsScreen.x,-billOfsScreen.y,0.0f,0.0f),
  611.             D3DXVECTOR4(-billOfsScreen.x, billOfsScreen.y,0.0f,0.0f),
  612.             D3DXVECTOR4(-billOfsScreen.x, billOfsScreen.y,0.0f,0.0f),
  613.             D3DXVECTOR4( billOfsScreen.x,-billOfsScreen.y,0.0f,0.0f),
  614.             D3DXVECTOR4( billOfsScreen.x, billOfsScreen.y,0.0f,0.0f),
  615.         };
  616.  
  617.         
  618.         const D3DSURFACE_DESC* pBackBufferSurfaceDesc = DXUTGetBackBufferSurfaceDesc();
  619.  
  620.         // Set constants across quad
  621.         for( int j=0; j < 6 ;++j )
  622.         {
  623.             // Scale to pixels
  624.             D3DXVec4Add( &pBlobVertex[posCount].pos, &blobScreenPos, &vPosOffset[j] );  
  625.             
  626.             pBlobVertex[posCount].pos.x *= pBackBufferSurfaceDesc->Width;             
  627.             pBlobVertex[posCount].pos.y *= pBackBufferSurfaceDesc->Height;
  628.             pBlobVertex[posCount].pos.x += 0.5f * pBackBufferSurfaceDesc->Width; 
  629.             pBlobVertex[posCount].pos.y += 0.5f * pBackBufferSurfaceDesc->Height;
  630.             
  631.             pBlobVertex[posCount].tCurr = vTexCoords[j];
  632.             pBlobVertex[posCount].tBack = D3DXVECTOR2((0.5f+pBlobVertex[posCount].pos.x)*(1.0f/pBackBufferSurfaceDesc->Width),
  633.                                                    (0.5f+pBlobVertex[posCount].pos.y)*(1.0f/pBackBufferSurfaceDesc->Height));
  634.             pBlobVertex[posCount].fSize = blobPos[i].size;
  635.             pBlobVertex[posCount].vColor = blobPos[i].color;
  636.  
  637.             posCount++;
  638.         }
  639.     }
  640.     g_pBlobVB->Unlock();
  641.  
  642.     return hr;
  643. }
  644.  
  645.  
  646. //--------------------------------------------------------------------------------------
  647. // This callback function will be called once at the beginning of every frame. This is the
  648. // best location for your application to handle updates to the scene, but is not 
  649. // intended to contain actual rendering calls, which should instead be placed in the 
  650. // OnFrameRender callback.  
  651. //--------------------------------------------------------------------------------------
  652. void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime )
  653. {
  654.     HRESULT hr;
  655.  
  656.     static bool bPaused = false;
  657.  
  658.     // Update the camera's position based on user input 
  659.     g_Camera.FrameMove( fElapsedTime );
  660.  
  661.     // Pause animatation if the user is rotating around
  662.     if( g_Camera.IsBeingDragged() && !DXUTIsTimePaused() )
  663.         DXUTPause( true, false );
  664.     if( !g_Camera.IsBeingDragged() && DXUTIsTimePaused() )
  665.         DXUTPause( false, false );
  666.     
  667.     // Get the projection & view matrix from the camera class
  668.     D3DXMATRIXA16 mWorldView;
  669.     D3DXMATRIXA16 mWorldViewProjection;
  670.     
  671.     D3DXMatrixMultiply( &mWorldView, g_Camera.GetWorldMatrix(), g_Camera.GetViewMatrix() );
  672.     D3DXMatrixMultiply( &mWorldViewProjection, &mWorldView, g_Camera.GetProjMatrix() );
  673.  
  674.     // Update the effect's variables.  Instead of using strings, it would 
  675.     // be more efficient to cache a handle to the parameter by calling 
  676.     // ID3DXEffect::GetParameterByName
  677.     V( g_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection ) );
  678.     
  679.     
  680.     // Animate the blobs
  681.     float pos = (float) ( 1.0f + cos( 2 * D3DX_PI * fTime/3.0f ) );
  682.     g_BlobPoints[1].pos.x = pos;
  683.     g_BlobPoints[2].pos.x = -pos;
  684.     g_BlobPoints[3].pos.y = pos;
  685.     g_BlobPoints[4].pos.y = -pos;
  686.     
  687.  
  688.     FillBlobVB( &mWorldView );
  689. }
  690.  
  691.  
  692. //--------------------------------------------------------------------------------------
  693. // This callback function will be called at the end of every frame to perform all the 
  694. // rendering calls for the scene, and it will also be called if the window needs to be 
  695. // repainted. After this function has returned, the sample framework will call 
  696. // IDirect3DDevice9::Present to display the contents of the next buffer in the swap chain
  697. //--------------------------------------------------------------------------------------
  698. void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime )
  699. {
  700.     HRESULT hr;
  701.     
  702.     LPDIRECT3DSURFACE9 apSurfOldRenderTarget[2] = { NULL, NULL };
  703.     LPDIRECT3DSURFACE9 pSurfOldDepthStencil = NULL;
  704.     LPDIRECT3DSURFACE9 pGBufSurf[4];
  705.  
  706.     // Clear the render target and the zbuffer 
  707.     V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );
  708.  
  709.     // Render the scene
  710.     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
  711.     {
  712.         // Get the initial device surfaces
  713.         pd3dDevice->GetRenderTarget( 0, &apSurfOldRenderTarget[0] );  // Only RT 0 should've been set.
  714.         V( pd3dDevice->GetDepthStencilSurface( &pSurfOldDepthStencil ) );
  715.  
  716.         // Turn off Z
  717.         V( pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE ) );
  718.         V( pd3dDevice->SetDepthStencilSurface( NULL ) );
  719.  
  720.  
  721.         // Clear the blank texture
  722.         LPDIRECT3DSURFACE9 pSurfBlank;
  723.         V( g_pTexScratch->GetSurfaceLevel( 0, &pSurfBlank ) );
  724.         V( pd3dDevice->SetRenderTarget( 0, pSurfBlank ) );
  725.         V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET , D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 ) );
  726.         SAFE_RELEASE( pSurfBlank );
  727.  
  728.         // clear temp textures
  729.         for( int i=0; i < 2; ++i )
  730.         {
  731.             V( g_pTexGBuffer[i]->GetSurfaceLevel( 0, &pGBufSurf[i] ) );
  732.             V( pd3dDevice->SetRenderTarget( 0, pGBufSurf[i] ) );
  733.             V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET , D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 ) );
  734.         }
  735.         for( int i = 2; i < 4; ++i )
  736.             V( g_pTexGBuffer[i]->GetSurfaceLevel( 0, &pGBufSurf[i] ) );
  737.  
  738.         V( pd3dDevice->SetStreamSource( 0, g_pBlobVB, 0, sizeof(SCREENVERTEX) ) );
  739.         V( pd3dDevice->SetFVF( SCREENVERTEX::FVF ) );
  740.  
  741.  
  742.         // Render the blobs
  743.         UINT iPass, nNumPasses;
  744.  
  745.         V( g_pEffect->SetTechnique( g_hBlendTech ) );
  746.         for( i=0; i < NUM_BLOBS; ++i )
  747.         {
  748.             // Copy bits off of render target into scratch surface [for blending].
  749.             V( g_pEffect->SetTexture( "g_tSourceBlob", g_pTexScratch ) );
  750.             V( g_pEffect->SetTexture( "g_tNormalBuffer", g_pTexGBuffer[0] ) );
  751.             V( g_pEffect->SetTexture( "g_tColorBuffer", g_pTexGBuffer[1] ) );
  752.  
  753.             if( SUCCEEDED( g_pEffect->Begin( &nNumPasses, 0 ) ) )
  754.             {
  755.                 for( iPass=0; iPass < nNumPasses; iPass++ )
  756.                 {
  757.                     for( int rt = 0; rt < g_nRtUsed; ++rt )
  758.                         V( pd3dDevice->SetRenderTarget( rt, g_aRTSet[iPass].apCopyRT[rt] ) );
  759.  
  760.                     V( g_pEffect->BeginPass( iPass ) );
  761.  
  762.                     V( pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, i*6, 2 ) );
  763.                     V( g_pEffect->EndPass() );
  764.                 }
  765.  
  766.                 V( g_pEffect->End() );
  767.             }
  768.  
  769.             // Render the blob
  770.             V( g_pEffect->SetTexture( "g_tSourceBlob", g_pTexBlob ) );
  771.             V( g_pEffect->SetTexture( "g_tNormalBuffer", g_pTexGBuffer[2] ) );
  772.             V( g_pEffect->SetTexture( "g_tColorBuffer", g_pTexGBuffer[3] ) );
  773.  
  774.             if( SUCCEEDED( g_pEffect->Begin( &nNumPasses, 0 ) ) )
  775.             {
  776.                 for( iPass=0; iPass < nNumPasses; iPass++ )
  777.                 {
  778.                     for( int rt = 0; rt < g_nRtUsed; ++rt )
  779.                         V( pd3dDevice->SetRenderTarget( rt, g_aRTSet[iPass].apBlendRT[rt] ) );
  780.  
  781.                     V( g_pEffect->BeginPass( iPass ) );
  782.  
  783.                     V( pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, i*6, 2 ) );
  784.                     V( g_pEffect->EndPass() );
  785.                 }
  786.  
  787.                 V( g_pEffect->End() );
  788.             }
  789.         }
  790.  
  791.         // Restore initial device surfaces
  792.         V( pd3dDevice->SetDepthStencilSurface( pSurfOldDepthStencil ) );
  793.  
  794.         for( int rt = 0; rt < g_nRtUsed; ++rt )
  795.             V( pd3dDevice->SetRenderTarget( rt, apSurfOldRenderTarget[rt] ) );
  796.  
  797.         // Light and composite blobs into backbuffer
  798.         V( g_pEffect->SetTechnique( "BlobLight" ) );
  799.         
  800.         V( g_pEffect->Begin( &nNumPasses, 0 ) );
  801.  
  802.         for( iPass=0; iPass < nNumPasses; iPass++ )
  803.         {
  804.             V( g_pEffect->BeginPass( iPass ) );
  805.  
  806.             for( i=0; i < NUM_BLOBS; ++i )
  807.             {
  808.                 V( g_pEffect->SetTexture( "g_tSourceBlob", g_pTexGBuffer[0] ) );
  809.                 V( g_pEffect->SetTexture( "g_tColorBuffer", g_pTexGBuffer[1] ) );
  810.                 V( g_pEffect->SetTexture( "g_tEnvMap", g_pEnvMap ) );
  811.                 V( g_pEffect->CommitChanges() );
  812.  
  813.                 V( RenderFullScreenQuad( 1.0f ) );
  814.             }
  815.             V( g_pEffect->EndPass() );
  816.         }
  817.  
  818.         V( g_pEffect->End() );
  819.  
  820.         RenderText();
  821.         V( g_HUD.OnRender( fElapsedTime ) );
  822.         
  823.         V( pd3dDevice->EndScene() );
  824.     }
  825.  
  826.     for( int rt = 0; rt < g_nRtUsed; ++rt )
  827.         SAFE_RELEASE( apSurfOldRenderTarget[rt] );
  828.     SAFE_RELEASE( pSurfOldDepthStencil );
  829.  
  830.     
  831.     for( int i=0; i < 4; ++i )
  832.     {
  833.         SAFE_RELEASE( pGBufSurf[i] );
  834.     }
  835. }
  836.  
  837.  
  838. //-----------------------------------------------------------------------------
  839. // Name: RenderFullScreenQuad()
  840. // Desc: Render a quad at the specified tranformed depth 
  841. //-----------------------------------------------------------------------------
  842. HRESULT RenderFullScreenQuad( float fDepth )
  843. {
  844.     SCREENVERTEX aVertices[4];
  845.  
  846.     IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
  847.     const D3DSURFACE_DESC* pBackBufferSurfaceDesc = DXUTGetBackBufferSurfaceDesc();
  848.  
  849.     aVertices[0].pos = D3DXVECTOR4( -0.5f, -0.5f, fDepth, fDepth );
  850.     aVertices[1].pos = D3DXVECTOR4( pBackBufferSurfaceDesc->Width - 0.5f, -0.5f, fDepth, fDepth );
  851.     aVertices[2].pos = D3DXVECTOR4( -0.5f, pBackBufferSurfaceDesc->Height - 0.5f, fDepth, fDepth );
  852.     aVertices[3].pos = D3DXVECTOR4( pBackBufferSurfaceDesc->Width - 0.5f, pBackBufferSurfaceDesc->Height - 0.5f, fDepth, fDepth );
  853.     
  854.     
  855.     aVertices[0].tCurr = D3DXVECTOR2( 0.0f, 0.0f );
  856.     aVertices[1].tCurr = D3DXVECTOR2( 1.0f, 0.0f );
  857.     aVertices[2].tCurr = D3DXVECTOR2( 0.0f, 1.0f );
  858.     aVertices[3].tCurr = D3DXVECTOR2( 1.0f, 1.0f );
  859.  
  860.     for (int i=0;i<4;++i)
  861.     {
  862.         aVertices[i].tBack = aVertices[i].tCurr;
  863.         aVertices[i].tBack.x += (1.0f/pBackBufferSurfaceDesc->Width);
  864.         aVertices[i].tBack.y += (1.0f/pBackBufferSurfaceDesc->Height);
  865.         aVertices[i].fSize = 0.0f;
  866.     }
  867.  
  868.  
  869.     pd3dDevice->SetFVF(SCREENVERTEX::FVF);
  870.     pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,aVertices,sizeof(SCREENVERTEX));
  871.     
  872.  
  873.     return S_OK;
  874. }
  875.  
  876.  
  877. //--------------------------------------------------------------------------------------
  878. // Render the help and statistics text. This function uses the ID3DXFont interface for 
  879. // efficient text rendering.
  880. //--------------------------------------------------------------------------------------
  881. void RenderText()
  882. {
  883.     // The helper object simply helps keep track of text position, and color
  884.     // and then it calls pFont->DrawText( g_pSprite, strMsg, -1, &rc, DT_NOCLIP, g_clr );
  885.     // If NULL is passed in as the sprite object, then it will work however the 
  886.     // pFont->DrawText() will not be batched together.  Batching calls will improves performance.
  887.     CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
  888.  
  889.     // Output statistics
  890.     txtHelper.Begin();
  891.     txtHelper.SetInsertionPos( 5, 5 );
  892.     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
  893.     txtHelper.DrawTextLine( DXUTGetFrameStats() );
  894.     txtHelper.DrawTextLine( DXUTGetDeviceStats() );
  895.  
  896.     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
  897.     
  898.     // Draw help
  899.     if( g_bShowHelp )
  900.     {
  901.         const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
  902.         txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*6 );
  903.         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
  904.         txtHelper.DrawTextLine( L"Controls (F1 to hide):" );
  905.  
  906.         txtHelper.SetInsertionPos( 40, pd3dsdBackBuffer->Height-15*5 );
  907.         txtHelper.DrawTextLine( L"Rotate model: Left mouse button\n"
  908.                                 L"Rotate camera: Right mouse button\n"
  909.                                 L"Zoom camera: Mouse wheel scroll\n" );
  910.     }
  911.     else
  912.     {
  913.         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
  914.         txtHelper.DrawTextLine( L"Press F1 for help" );
  915.     }
  916.     txtHelper.End();
  917. }
  918.  
  919.  
  920. //--------------------------------------------------------------------------------------
  921. // Before handling window messages, the sample framework passes incoming windows 
  922. // messages to the application through this callback function. If the application sets 
  923. // *pbNoFurtherProcessing to TRUE, then the sample framework will not process this message.
  924. //--------------------------------------------------------------------------------------
  925. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing )
  926. {
  927.     // Give the dialogs a chance to handle the message first
  928.     *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
  929.     if( *pbNoFurtherProcessing )
  930.         return 0;
  931.  
  932.     // Pass all remaining windows messages to camera so it can respond to user input
  933.     g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
  934.  
  935.     return 0;
  936. }
  937.  
  938.  
  939. //--------------------------------------------------------------------------------------
  940. // As a convenience, the sample framework inspects the incoming windows messages for
  941. // keystroke messages and decodes the message parameters to pass relevant keyboard
  942. // messages to the application.  The framework does not remove the underlying keystroke 
  943. // messages, which are still passed to the application's MsgProc callback.
  944. //--------------------------------------------------------------------------------------
  945. void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown )
  946. {
  947.     if( bKeyDown )
  948.     {
  949.         switch( nChar )
  950.         {
  951.             case VK_F1: g_bShowHelp = !g_bShowHelp; break;
  952.             case VK_F2: if( DXUTIsTimePaused() ) DXUTPause(false, false);
  953.         }
  954.     }
  955. }
  956.  
  957.  
  958. //--------------------------------------------------------------------------------------
  959. // Handles the GUI events
  960. //--------------------------------------------------------------------------------------
  961. void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl )
  962. {
  963.     switch( nControlID )
  964.     {
  965.         case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break;
  966.         case IDC_TOGGLEREF:        DXUTToggleREF(); break;
  967.         case IDC_CHANGEDEVICE:     DXUTSetShowSettingsDialog( !DXUTGetShowSettingsDialog() ); break;
  968.     }
  969. }
  970.  
  971.  
  972. //--------------------------------------------------------------------------------------
  973. // This callback function will be called immediately after the Direct3D device has 
  974. // entered a lost state and before IDirect3DDevice9::Reset is called. Resources created
  975. // in the OnResetDevice callback should be released here, which generally includes all 
  976. // D3DPOOL_DEFAULT resources. See the "Lost Devices" section of the documentation for 
  977. // information about lost devices.
  978. //--------------------------------------------------------------------------------------
  979. void CALLBACK OnLostDevice()
  980. {
  981.     if( g_pFont )
  982.         g_pFont->OnLostDevice();
  983.     if( g_pEffect )
  984.         g_pEffect->OnLostDevice();
  985.  
  986.     SAFE_RELEASE( g_pTextSprite );
  987.     SAFE_RELEASE( g_pBlobVB );
  988.     SAFE_RELEASE( g_pTexScratch );
  989.     SAFE_RELEASE( g_pTexBlob );
  990.     SAFE_RELEASE( g_pEnvMap );
  991.  
  992.     for( int p = 0; p < 2; ++p )
  993.         for( int i = 0; i < 2; ++i )
  994.         {
  995.             SAFE_RELEASE( g_aRTSet[p].apCopyRT[i] );
  996.             SAFE_RELEASE( g_aRTSet[p].apBlendRT[i] );
  997.         }
  998.  
  999.     for( UINT i=0; i < 4; i++ )
  1000.     {
  1001.         SAFE_RELEASE( g_pTexGBuffer[i] );
  1002.     }
  1003. }
  1004.  
  1005.  
  1006. //--------------------------------------------------------------------------------------
  1007. // This callback function will be called immediately after the Direct3D device has 
  1008. // been destroyed, which generally happens as a result of application termination or 
  1009. // windowed/full screen toggles. Resources created in the OnCreateDevice callback 
  1010. // should be released here, which generally includes all D3DPOOL_MANAGED resources. 
  1011. //--------------------------------------------------------------------------------------
  1012. void CALLBACK OnDestroyDevice()
  1013. {
  1014.     SAFE_RELEASE(g_pEffect);
  1015.     SAFE_RELEASE(g_pFont);
  1016. }
  1017.  
  1018.  
  1019.  
  1020.